package com.ruiysoft.service.order.facade;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruiysoft.entity.order.OrderDetail;
import com.ruiysoft.entity.order.OrderLogistics;
import com.ruiysoft.entity.order.OrderMain;
import com.ruiysoft.entity.order.OrderPayTransaction;
import com.ruiysoft.entity.order.OrderReceiverInfo;
import com.ruiysoft.entity.order.OrderSkuInfo;
import com.ruiysoft.entity.order.OrderSkuPic;
import com.ruiysoft.service.order.IOrderDetailService;
import com.ruiysoft.service.order.IOrderLogisticsService;
import com.ruiysoft.service.order.IOrderMainService;
import com.ruiysoft.service.order.IOrderPayTransactionService;
import com.ruiysoft.service.order.IOrderReceiverInfoService;
import com.ruiysoft.service.order.IOrderSkuInfoService;
import com.ruiysoft.service.order.IOrderSkuPicService;
import com.ruiysoft.service.order.facade.util.OrderEntityWrapperUtil;
import com.ruiysoft.util.NumberConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Date : 2018/11/10
 *
 * @author WenJialu
 */
@Component
public class OrderPubFacade {

    @Resource
    private IOrderMainService orderMainService;
    @Resource
    private IOrderDetailService orderDetailService;
    @Resource
    private IOrderSkuInfoService orderSkuInfoService;
    @Resource
    private IOrderSkuPicService orderSkuPicService;
    @Resource
    private IOrderLogisticsService orderLogisticsService;
    @Resource
    private IOrderPayTransactionService orderPayTransactionService;
    @Resource
    private IOrderReceiverInfoService orderReceiverInfoService;

    /**
     * 订单主表信息
     *
     * @param orderNo
     * @return
     */
    public OrderMain getOrderMain(String orderNo) {
        return orderMainService.getOne(new QueryWrapper<OrderMain>().eq("order_no", orderNo));
    }

    /**
     * 订单详情
     *
     * @param orderNo
     * @return
     */
    public List<OrderDetail> getOrderDetails(String orderNo) {
        return orderDetailService.list(new QueryWrapper<OrderDetail>().eq("order_no", orderNo).orderBy(false, false, "sku_id"));
    }

    /**
     * 订单sku信息
     *
     * @param orderNo
     * @return
     */
    public List<OrderSkuInfo> getOrderSkuInfos(String orderNo) {
        return orderSkuInfoService.list(new QueryWrapper<OrderSkuInfo>().eq("order_no", orderNo).orderBy(false, false, "sku_id"));
    }

    /**
     * returnMap key - sku_id, value - List<OrderSkuPic>
     *
     * @param orderNo
     * @return
     */
    public Map<Long, List<OrderSkuPic>> getOrderSkuPics(String orderNo) {
        List<OrderSkuPic> orderSkuPics = orderSkuPicService.list(new QueryWrapper<OrderSkuPic>().eq("order_no", orderNo).orderBy(false, false, "sku_id").orderBy(true, false, "sort"));
        if (null == orderSkuPics || orderSkuPics.isEmpty()) {
            return Collections.emptyMap();
        } else {
            return orderSkuPics.stream().collect(Collectors.groupingBy(OrderSkuPic::getSkuId, Collectors.toList()));
        }
    }

    /**
     * 订单sku首张图片
     * returnMap key - sku_id, value - path
     *
     * @param orderNo
     * @return
     */
    public Map<Long, String> getOrderSkuFirstPics(String orderNo) {
        List<OrderSkuPic> orderSkuPics = orderSkuPicService.list(new QueryWrapper<OrderSkuPic>().eq("order_no", orderNo).eq("sort", NumberConst.ZERO_INT).orderBy(false, false, "sku_id"));
        if (null == orderSkuPics || orderSkuPics.isEmpty()) {
            return Collections.emptyMap();
        } else {
            return orderSkuPics.stream().filter(orderSkuPic -> StringUtils.isNotBlank(orderSkuPic.getPath())).collect(Collectors.toMap(OrderSkuPic::getSkuId, OrderSkuPic::getPath));
        }
    }


    /**
     * 订单物流信息
     *
     * @param orderNo
     * @return
     */
    public List<OrderLogistics> getOrderLogisticsList(String orderNo) {
        return orderLogisticsService.list(new QueryWrapper<OrderLogistics>().eq("order_no", orderNo).orderBy(false, false, "update_time DESC"));
    }


    IPage<OrderMain> getOrderMainPage(String orderNo, Long userId, Long shopId, Integer orderDbStatus, List<Integer> orderDbStatusIn, Integer pageIndex, Integer pageSize) {
        if (null == pageIndex || null == pageSize) {
            return new Page<OrderMain>().setRecords(Collections.emptyList());
        } else {
            return orderMainService.page(new Page<>(pageIndex, pageSize), OrderEntityWrapperUtil.INSTANCE.createOrderMainWrapper(orderNo, userId, shopId, orderDbStatus, orderDbStatusIn));
        }
    }

    public OrderPayTransaction getOrderPayTransaction(String sysTransactionNo) {
        return orderPayTransactionService.getOne(new QueryWrapper<OrderPayTransaction>().eq("sys_transaction_no", sysTransactionNo).last(" LIMIT 1 "));
    }

    /**
     * 订单 收货人信息
     *
     * @param orderNo
     * @return
     */
    public OrderReceiverInfo getOrderReceiverInfo(String orderNo) {
        return orderReceiverInfoService.getOne(new QueryWrapper<OrderReceiverInfo>().eq("order_no", orderNo).last(" LIMIT 1 "));
    }
}
